package ESP.Control.B;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import ESP.Dao.BaseDao;
import ESP.Util.IDFactory;
import ESP.Util.UploadConfigurationRead;
import ESP.VO.Data;
import ESP.VO.Role;

import com.alibaba.fastjson.JSONArray;

/**
 * 
 * @author 程尧
 * @version Report
 *
 */
public class b02 {
	
	@Resource
	public BaseDao baseDao;
	/**
	 * 返回信息
	 */
	private JSONArray rows;
	private Map<String, String> message = new HashMap<String, String>();
	/**
	 * 用户信息
	 */
	private String user_role_id;
	private String user_id;
	/**
	 * 表头查询
	 * @return
	 */
	
	private String typeId;
	private String dataName;
	private String dataNote;//与task相关
	/**
	 * file
	 */
	private File[] upload;// 实际上传文件
    private String[] uploadContentType; // 文件的内容类型
    private String[] uploadFileName; // 上传文件名
    private List<Data> uploadFiles = new ArrayList<Data>();
    /**
     * 下载地址
     */
    private String downloadPath;
    /**
     * DATA
     */
    private String data_id;
    private String data_name;
	private String data_type_id;//
	private Date data_cretime;
	private String data_path;
	private String data_owner_id;
	private String data_note;
	private String data_flag;//数据标志1\2\3
	private String data_report_flag;
	
	/**
	 * 查询表头
	 * @return
	 */
	public String queryCoursewareHead(){
		List list = baseDao.queryDataHeadB02(dataName, typeId, dataNote);
		this.rows = baseDao.toJson(list);
		return "queryCoursewareHead";
	}
	
	/**
	 * 上传
	 * @return
	 */
	public String uploadCourseware() {
		System.out.println("【上传课程资料开始……】");
		try {
			String relativePath = 
				"/" + UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim();
			String targetDirectory = ServletActionContext.getServletContext().getRealPath(relativePath);// 获得路径
			for (int i = 0; i < upload.length; i++) {
				String fileName = uploadFileName[i];// 上传的文件名
				String type = UploadConfigurationRead.getExt(fileName);// 文件类型
				String realName = IDFactory.getID();// 保存的文件名称
				
				// 在把路径写入数据库即可
				this.data_id = realName;
				this.data_name = fileName;
				this.data_type_id = type;
				this.data_path = relativePath+"/"+realName+"."+type;
				this.data_owner_id = this.user_id;
				this.data_flag = Data.TEACHER_DATA;
				this.data_report_flag = Data.NOT_REPORT;
				
				System.out.println("data_flag:"+data_flag);
				baseDao.addData(data_id, data_name, data_type_id, data_cretime, data_path, data_owner_id,
						data_note, data_flag, data_report_flag);
				File target = new File(targetDirectory, realName+"."+type);
				FileUtils.copyFile(upload[i], target);// 上传至服务器的目录，一般都这样操作
				System.out.println("【上传课程资料成功】");
				baseDao.setReturnInfo(message, "Y", "上传教学资料成功!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			baseDao.setReturnInfo(message, "N", "上传教学资料失败!");
			return "uploadCourseware";
		}
		return "uploadCourseware";
	}
	/**
	 * 删除课程资料文件
	 * @return
	 */
	public String removeCourseware(){
		System.out.println("removeCourseware开始");
		int i = baseDao.removeData(this.data_id);
		if (i == 1) {
			baseDao.setReturnInfo(message, "Y", "删除课程资料成功!");
		}else {
			baseDao.setReturnInfo(message, "N", "删除教学资料失败!");
		}
		return "removeCourseware";
	}
	/**
	 * 修改
	 * @return
	 */
//	public String updateCourseware(){
//		System.out.println("updateCourseware开始");
//		String relativePath = "/" + UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim();
//		String targetDirectory = ServletActionContext.getServletContext().getRealPath(relativePath);// 获得路径
//		try {
//			for (int i = 0; i < upload.length; i++) {
//				String fileName = uploadFileName[i];// 上传的文件名
//				String type = UploadConfigurationRead.getExt(fileName);// 文件类型
//				String realName = this.data_id;// 保存的文件名称
//				
//				// 在把路径写入数据库即可
//				this.data_name = fileName;
//				this.data_type_id = type;
//				this.data_path = relativePath+"/"+realName+"."+type;
//				this.data_owner_id = this.user_id;
//				this.data_flag = Data.TEACHER_DATA;
//				this.data_report_flag = Data.NOT_REPORT;
//				int j = baseDao.updataData(data_id, data_name, data_type_id, data_cretime,
//						data_path, data_owner_id, data_note, data_flag, data_report_flag);
//				if (j == 0) {
//					throw new RuntimeException("修改Courseware失败!");
//				}
//				File target = new File(targetDirectory, realName+"."+type);
//				FileUtils.copyFile(upload[i], target);// 上传至服务器的目录，一般都这样操作
//				System.out.println("【上传并修改课程资料成功……】");
//				return "updateCourseware";
//			}
//		} catch (Exception e) {
//			throw new RuntimeException("修改Courseware失败!");
//		}
//		return "updateCourseware";
//	}
	/**
	 * 查询文件类型
	 * @return
	 */
	public String queryTask(){
		List list = baseDao.querySqlForList("select task_id,task_name from esp_task");
		this.rows = baseDao.toJson(list);
		return "queryTask";
	}
	//===========set,get
	public BaseDao getBaseDao() {
		return baseDao;
	}

	public void setBaseDao(BaseDao baseDao) {
		this.baseDao = baseDao;
	}

	public JSONArray getRows() {
		return rows;
	}

	public void setRows(JSONArray rows) {
		this.rows = rows;
	}

	public String getUser_role_id() {
		return user_role_id;
	}

	public void setUser_role_id(String userRoleId) {
		user_role_id = userRoleId;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String userId) {
		user_id = userId;
	}

	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	public File[] getUpload() {
		return upload;
	}
	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getData_id() {
		return data_id;
	}
	public void setData_id(String dataId) {
		data_id = dataId;
	}
	public String getData_type_id() {
		return data_type_id;
	}
	public void setData_type_id(String dataTypeId) {
		data_type_id = dataTypeId;
	}
	public Date getData_cretime() {
		return data_cretime;
	}
	public void setData_cretime(Date dataCretime) {
		data_cretime = dataCretime;
	}
	public String getData_path() {
		return data_path;
	}
	public void setData_path(String dataPath) {
		data_path = dataPath;
	}
	public String getData_owner_id() {
		return data_owner_id;
	}
	public void setData_owner_id(String dataOwnerId) {
		data_owner_id = dataOwnerId;
	}
	public String getData_note() {
		return data_note;
	}
	public void setData_note(String dataNote) {
		data_note = dataNote;
	}
	public String getData_flag() {
		return data_flag;
	}
	public void setData_flag(String dataFlag) {
		data_flag = dataFlag;
	}
	public String getData_report_flag() {
		return data_report_flag;
	}
	public void setData_report_flag(String dataReportFlag) {
		data_report_flag = dataReportFlag;
	}
	public String getData_name() {
		return data_name;
	}
	public void setData_name(String dataName) {
		data_name = dataName;
	}
	public String getDownloadPath() {
		return downloadPath;
	}
	public void setDownloadPath(String downloadPath) {
		this.downloadPath = downloadPath;
	}
	public Map<String, String> getMessage() {
		return message;
	}
	public void setMessage(Map<String, String> message) {
		this.message = message;
	}
	public String getDataName() {
		return dataName;
	}
	public void setDataName(String dataName) {
		this.dataName = dataName;
	}
	public String getDataNote() {
		return dataNote;
	}
	public void setDataNote(String dataNote) {
		this.dataNote = dataNote;
	}
	public List<Data> getUploadFiles() {
		return uploadFiles;
	}
	public void setUploadFiles(List<Data> uploadFiles) {
		this.uploadFiles = uploadFiles;
	}
}
